//
//  GoodsCollectionViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/6.
//

import UIKit

class GoodsCollectionViewCell: UICollectionViewCell {
    var bgView: UIView!
    var carImageView: UIImageView!
    var specialPriceBtn: UIButton!
    var nameLab: UILabel!
    var priceLab: UILabel!
    var originalPriceLab: UILabel!
    var collectBtn: UIButton!
    var model: VehicleLisetModel!
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
        self.clipsToBounds = true
        
        self.initUI()
        
    }
    func initUI(){
        self.bgView = UIView(frame: .zero)
        self.bgView.layer.cornerRadius = 8
        self.bgView.clipsToBounds = true
        self.bgView.backgroundColor = Theme.viewBkgGrayColor
        self.contentView.addSubview(self.bgView)
        
        self.carImageView = UIImageView(frame: .zero)
        self.carImageView.layer.cornerRadius = 8
        self.carImageView.clipsToBounds = true
        self.carImageView.backgroundColor = .clear
        self.carImageView.contentMode = .scaleAspectFill
        self.carImageView.clipsToBounds = true
        self.bgView.addSubview(self.carImageView)
        
        self.nameLab = UILabel(frame: .zero)
        self.nameLab.textColor = Theme.titleLabColor
        self.nameLab.font = UIFont(name: Theme.PingFangSCMedium, size: 13)
        self.nameLab.numberOfLines = 2
        self.bgView.addSubview(self.nameLab)
        
        self.priceLab = UILabel(frame: .zero)
        self.priceLab.textColor = Theme.mainColor
        self.priceLab.font = UIFont(name: Theme.PingFangSCMedium, size: 16)
        self.bgView.addSubview(self.priceLab)
        
        
        self.originalPriceLab = UILabel(frame: .zero)
        self.originalPriceLab.textColor = UIColor(hex: 0xAFBED7)
        self.originalPriceLab.font = UIFont(name: Theme.PingFangSCRegular, size: 11)
        self.bgView.addSubview(self.originalPriceLab)
        
        self.specialPriceBtn = UIButton(frame: CGRect(x: 14, y: 0, width: self.frame.size.width - 28, height: 60))
        self.specialPriceBtn.backgroundColor = Theme.redColor
        self.specialPriceBtn.setTitleColor(.white, for: .normal)
        self.specialPriceBtn.isUserInteractionEnabled = false
        self.specialPriceBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 11)
        self.specialPriceBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 3, bottom: 0, right: 3)
        self.carImageView.addSubview(self.specialPriceBtn)
        self.specialPriceBtn.setTitle(MMLocalizedString("special", comment: "特价"), for: .normal)
        
        self.collectBtn = UIButton(frame: .zero)
        self.collectBtn.setup_image(image: UIImage(named: "icon_collect_normal"))
        self.collectBtn.setImage(UIImage(named: "icon_collect_select"), for: .selected)
        self.collectBtn.addTarget(self, action: #selector(collectBtnAntion), for: .touchUpInside)
        self.bgView.addSubview(self.collectBtn)
        
        
        //        self.nameLab.text = "Volkswagen 2023 e-Golf 330"
        //        self.priceLab.text = "HK＄3,000,00"
        //        self.originalPriceLab.text = "HK＄3,000,00"
        
        self.addLayout()
        
    }
    
    func addLayout(){
        
        self.bgView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(210)
            make.bottom.equalTo(0)
        }
        
        self.carImageView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(112)
        }
        
        self.specialPriceBtn.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.right.equalTo(0)
            make.width.equalTo(40)
            make.height.equalTo(18)
        }
        
        self.nameLab.snp.makeConstraints { make in
            make.top.equalTo(self.carImageView.snp.bottom).offset(5)
            make.left.equalTo(8)
            make.right.equalTo(-8)
            make.height.greaterThanOrEqualTo(40)
        }
        
        self.priceLab.snp.makeConstraints { make in
            make.top.equalTo(self.nameLab.snp.bottom).offset(0)
            make.left.equalTo(8)
            make.right.equalTo(-8)
            make.height.greaterThanOrEqualTo(20)
        }
        
        self.originalPriceLab.snp.makeConstraints { make in
            make.top.equalTo(self.priceLab.snp.bottom)
            make.left.equalTo(8)
            make.right.equalTo(30)
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalTo(-10)
        }
        
        self.collectBtn.snp.makeConstraints { make in
            make.right.equalTo(-8)
            make.height.equalTo(20)
            make.width.equalTo(20)
            make.bottom.equalTo(-10)
        }
    }
    
    
    //setModel
    func setModel(model: VehicleLisetModel){
        
        self.model = model
        
        if let url = URL.init(string: model.backgroundImage ?? (model.productImage ?? "")) {
            self.carImageView.sd_setImage(with: url, placeholderImage: UIImage.init(named: "icon_default_place_v"))
        }else{
            self.carImageView.image = UIImage.init(named: "icon_default_place_v")
        }
        self.nameLab.text = model.name ?? ""
        
        let price = XTManageCenter.showPriceInComma(source: (model.price ?? 0).cleanZero)
        let text = NSMutableAttributedString(string: String(format: "%@%@", XTManageCenter.currentSign(),price))
        text.addAttribute(NSAttributedStringKey.font, value: UIFont.init(name: Theme.PingFangSCRegular, size: 11)!, range: NSMakeRange(0, (XTManageCenter.currentSign().count)))
        self.priceLab.attributedText = text
        
        if (model.originalPrice ?? 0) > 0 {
            let originalPrice = XTManageCenter.showPriceInComma(source: (model.originalPrice ?? 0).cleanZero)
            let tempOriginPrice = String(format: "%@%@", XTManageCenter.currentSign(),originalPrice)
            
            let originPriceString = NSMutableAttributedString.init(string: tempOriginPrice)
            originPriceString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSNumber.init(value: 1), range: NSRange(location: 0, length: originPriceString.length))
            self.originalPriceLab.attributedText = originPriceString
        }else{
            self.originalPriceLab.attributedText = nil
            self.originalPriceLab.text = ""
        }
        
        if model.isSpecial == true {
            self.originalPriceLab.snp.updateConstraints { make in
                make.height.greaterThanOrEqualTo(20)
            }
            self.specialPriceBtn.isHidden = false
            self.specialPriceBtn.setTitle(MMLocalizedString("special", comment: "特价"), for: .normal)
        }else{
            self.specialPriceBtn.isHidden = true
            self.originalPriceLab.snp.updateConstraints { make in
                make.height.greaterThanOrEqualTo(0)
            }
        }
        self.collectBtn.isSelected = model.favorite ?? false
        self.specialPriceBtn.titleLabel?.sizeToFit()
        let tempW = self.specialPriceBtn.titleLabel?.frame.size.width ?? 0
        self.specialPriceBtn.snp.updateConstraints { make in
            make.width.equalTo(tempW + 6)
        }
        self.updateConstraints()
    }
    
    
    ///商城列表
    func setStoreModel(storeModel: VehicleLisetModel){
        self.model = storeModel
        if let url = URL(string: model.productImage ?? "") {
            self.carImageView.setImageWith(url, placeholder: UIImage.init(named: "icon_default_place_v"))
        }else{
            self.carImageView.image = UIImage.init(named: "icon_default_place_v")
        }
        self.priceLab.attributedText = nil
        self.nameLab.text = model.name ?? ""
        self.specialPriceBtn.isHidden = true
        let type = self.getProductType()
        switch type {
            case .time:
                self.specialPriceBtn.isHidden = false
                self.specialPriceBtn.backgroundColor = UIColor(hex: 0xFED764)
                self.specialPriceBtn.setTitle(MMLocalizedString("limited_time", comment: "限时"), for: .normal)

                let price = XTManageCenter.showPriceInComma(source: (model.promotionPrice ?? 0).cleanZero)
                let text = NSMutableAttributedString(string: String(format: "%@%@", XTManageCenter.currentSign(),price))
                text.addAttribute(NSAttributedStringKey.font, value: UIFont.init(name: Theme.PingFangSCRegular, size: 11)!, range: NSMakeRange(0, (XTManageCenter.currentSign().count)))
                self.priceLab.attributedText = text
        case .vip:
                let price = XTManageCenter.showPriceInComma(source: (model.memberPrice ?? 0).cleanZero)
                let text = NSMutableAttributedString(string: String(format: "%@%@", XTManageCenter.currentSign(),price))
                text.addAttribute(NSAttributedStringKey.font, value: UIFont.init(name: Theme.PingFangSCRegular, size: 11)!, range: NSMakeRange(0, (XTManageCenter.currentSign().count)))
                self.priceLab.attributedText = text
                
                self.specialPriceBtn.isHidden = false
                self.specialPriceBtn.backgroundColor = Theme.titleLabColor
                self.specialPriceBtn.setTitle("VIP", for: .normal)
            case .discount:
                let price = XTManageCenter.showPriceInComma(source: (model.discountPrice ?? 0).cleanZero)
                let text = NSMutableAttributedString(string: String(format: "%@%@", XTManageCenter.currentSign(),price))
                text.addAttribute(NSAttributedStringKey.font, value: UIFont.init(name: Theme.PingFangSCRegular, size: 11)!, range: NSMakeRange(0, (XTManageCenter.currentSign().count)))
                self.priceLab.attributedText = text
                
                self.specialPriceBtn.isHidden = false
                self.specialPriceBtn.backgroundColor = Theme.redColor
                self.specialPriceBtn.setTitle(MMLocalizedString("special", comment: "特价"), for: .normal)
            default:
                let price = XTManageCenter.showPriceInComma(source: (model.price ?? 0).cleanZero)
                let text = NSMutableAttributedString(string: String(format: "%@%@", XTManageCenter.currentSign(),price))
                text.addAttribute(NSAttributedStringKey.font, value: UIFont.init(name: Theme.PingFangSCRegular, size: 11)!, range: NSMakeRange(0, (XTManageCenter.currentSign().count)))
                self.priceLab.attributedText = text
                
                self.specialPriceBtn.isHidden = true
        }
        
        self.specialPriceBtn.titleLabel?.sizeToFit()
        let tempW = self.specialPriceBtn.titleLabel?.frame.size.width ?? 0
        self.specialPriceBtn.snp.updateConstraints { make in
            make.width.equalTo(tempW + 6)
        }
        self.collectBtn.isSelected = storeModel.isFavorite ?? false
        
        
        //划线价
        if type == .normal {
            self.originalPriceLab.attributedText = nil
            self.originalPriceLab.isHidden = true
            self.originalPriceLab.snp.updateConstraints { make in
                make.height.greaterThanOrEqualTo(0)
            }
        }else{
            //取price
            self.originalPriceLab.isHidden = false
            let originalPrice = XTManageCenter.showPriceInComma(source: (model.price ?? 0).cleanZero)
            let tempOriginPrice = String(format: "%@%@", XTManageCenter.currentSign(),originalPrice)
            let originPriceString = NSMutableAttributedString.init(string: tempOriginPrice)
            originPriceString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSNumber.init(value: 1), range: NSRange(location: 0, length: originPriceString.length))
            self.originalPriceLab.attributedText = originPriceString
            self.originalPriceLab.snp.updateConstraints { make in
                make.height.greaterThanOrEqualTo(20)
            }
        }
        self.updateConstraints()
    }
    
    ///获取商品类型
    ///price、discountPrice、memberPrice哪个价格最低显示哪个，如果是会员价最低标签就是vip，如果折扣价最低标签就是特价
    private func getProductType() -> ProductType {
        if self.model.isLimitedTime ?? false == true {
            return .time
        }
        let price = self.model.price ?? 0
        let discountPrice = self.model.discountPrice ?? 0
        let memberPrice = self.model.memberPrice ?? 0
        if discountPrice == 0 && memberPrice == 0 {
            //普通商品
            return .normal
        } else if discountPrice > 0 && memberPrice > 0 {
            if discountPrice <= memberPrice {
                //特价
                return .discount
            }else{
                //会员
                return .vip
            }
        } else if discountPrice > 0 && discountPrice < price{
            return .discount
        }else if memberPrice > 0 && memberPrice < price {
            return .vip
        }
        return .normal
    }
    
    //限时抢购
    func setFlashSaleModel(model: VehicleLisetModel){
        self.collectBtn.isHidden = true
        self.model = model
        self.carImageView.sd_setImage(with: URL(string: model.productImage ?? ""))
        self.nameLab.text = model.name ?? ""
        
        let price = XTManageCenter.showPriceInComma(source: (model.promotionPrice ?? 0).cleanZero)
        let text = NSMutableAttributedString(string: String(format: "%@%@", XTManageCenter.currentSign(),price))
        text.addAttribute(NSAttributedStringKey.font, value: UIFont.init(name: Theme.PingFangSCRegular, size: 11)!, range: NSMakeRange(0, (XTManageCenter.currentSign().count)))
        self.priceLab.attributedText = text
        
        let originalPrice = XTManageCenter.showPriceInComma(source: (model.originalPrice ?? 0).cleanZero)
        let tempOriginPrice = String(format: "%@%@", XTManageCenter.currentSign(),originalPrice)
        let originPriceString = NSMutableAttributedString.init(string: tempOriginPrice)
        originPriceString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSNumber.init(value: 1), range: NSRange(location: 0, length: originPriceString.length))
        self.originalPriceLab.attributedText = originPriceString
        
        self.specialPriceBtn.isHidden = true
        self.originalPriceLab.snp.updateConstraints { make in
            make.height.greaterThanOrEqualTo(0)
        }
        self.collectBtn.isSelected = model.isFavorite ?? false
        
        self.updateConstraints()
    }
    
    
    //MARK: - Antion
    @objc func collectBtnAntion(){
        //self.collectBtn.isSelected = !self.collectBtn.isSelected
        self.model?.favoriteBlock?()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
